//
// Created by 11067 on 2023/3/6.
//

#include "utils.h"
static char to_lower(char c) {
    if (c >= 'a' && c <= 'z') {
        return c;
    }
    if (c >= 'A' && c <= 'Z') {
        return (char) ((int) c + 'a' - 'A');
    }
    return 0;
}

/*
 * read from "str_in"
 * put words into "words"
 */
//int str2words(std::string &str_in, std::set<std::string> &words) {
//    for (int i = 0; i < str_in.length(); i++) {
//        if (!is_alpha(str_in[i])) {
//            continue;
//        }
//        int j;
//        std::vector<char> word_v;
//        for (j = i; j < str_in.length() && is_alpha(str_in[j]); j++) {
//            word_v.push_back(str_in[j]);
//        }
//        std::string word;
//        word.assign(word_v.begin(), word_v.end());
//        words.insert(word);
//        i = j - 1;
//    }
//    return 0;
//}


int file2str(std::string file, std::string &str) {
    std::ifstream fin(file, std::ios::in);
    str = std::string((std::istreambuf_iterator<char>(fin)),
                      std::istreambuf_iterator<char>());
    fin.close();
    return 0;
}

int file2wordlist(std::string file, std::vector<std::string> &wordlist) {
    std::string str_in;
    file2str(file, str_in);
    for (int i = 0; i < str_in.length(); i++) {
        if (!is_alpha(str_in[i])) {
            continue;
        }
        int j;
        std::vector<char> word_v;
        for (j = i; j < str_in.length() && is_alpha(str_in[j]); j++) {
            word_v.push_back(str_in[j]);
        }
        std::string word;
        word.assign(word_v.begin(), word_v.end());
        wordlist.push_back(word);
        i = j - 1;
    }
    return 0;
}

void trim(std::string &str) {
    while (str.length() > 0 && !is_alpha(str[0])) {
        str.erase(0, 1);
    }
    while (str.length() > 0 && !is_alpha(str[str.length() - 1])) {
        str.pop_back();
    }
}


